package com.glacier.cloud.order.service.impl;

import com.glacier.cloud.order.entity.dto.cmd.CartRemoveCmd;
import com.glacier.cloud.order.entity.dto.cmd.OrderCreatCmd;
import com.glacier.cloud.order.entity.dto.cmd.ProductCmd;
import com.glacier.cloud.order.remote.CartClientService;
import com.glacier.cloud.order.service.CartService;
import com.glacier.framework.component.dto.response.Response;
import com.glacier.framework.component.dto.response.StatusCode;
import com.glacier.framework.component.exception.BizException;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * date 2024-01-06 16:38
 *
 * @author glacier
 * @version 1.0
 */
@Service
public class CartServiceImpl implements CartService {
    @Resource
    private CartClientService cartClientService;

    @Override
    public void removeProduct(OrderCreatCmd command) {
        List<Long> productSkuIds = command.getProducts()
                .stream()
                .map(ProductCmd::getProductSkuId)
                .collect(Collectors.toList());
        Response<Integer> removeProduct = cartClientService.removeProduct(
                new CartRemoveCmd(command.getCustomerUserId(), productSkuIds));
        if (!StatusCode.isSuccess(removeProduct.getCode())) {
            throw new BizException("从购物车移除商品失败");
        }
    }
}
